﻿/*
 * bGuiLabel.cs -- bGui Form Class
 * The bGui.Form1 Class, with support for bGui.
 * 
 * New BSD License (BSD)
 * Copyright (c) 2012, Karim Alibhai
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace bGui
{
    public sealed class bGuiForm
    {
        public string Name;
        public Form1 fWindow;

        public bGuiForm(string _Name, Form1 _fWindow)
        {
            Name = _Name;
            fWindow = _fWindow;
        }

        // Appending and removing controls
        public void Append(bGuiControl hControl)
        {
            Control c = new Control();

            switch (hControl.GetStrType())
            {
                case "Label":
                    c = ((bGuiLabel)hControl).hControl;
                    break;

                default:
                    return;
            }

            fWindow.Controls.Add(c);
        }
        public void Remove(bGuiControl hControl)
        {
            Control c = new Control();

            switch (hControl.GetStrType())
            {
                case "Label":
                    c = ((bGuiLabel)hControl).hControl;
                    break;

                default:
                    return;
            }

            fWindow.Controls.Remove(c);
        }

        // Dynamically set all supported properties
        public void SetProperties(JObject json)
        {
            foreach (KeyValuePair<string,JToken> item in json)
            {
                if (item.Key.StartsWith("."))
                {
                    switch (item.Key)
                    {
                        case ".Location":
                            if (item.Value.ToString().Contains(",") && item.Value.ToString().Split(',').Length == 2)
                            {
                                string val = item.Value.ToString().Trim();
                                fWindow.Location = new Point(int.Parse(val.Split(',')[0]), int.Parse(val.Split(',')[1]));
                                fWindow.StartPosition = FormStartPosition.Manual;
                            }
                            break;

                        case ".Size":
                            if (item.Value.ToString().Contains("x") && item.Value.ToString().Split('x').Length == 2)
                            {
                                string val = item.Value.ToString().Trim();
                                fWindow.Size = new Size(int.Parse(val.Split('x')[0]), int.Parse(val.Split('x')[1]));
                            }
                            break;

                        default:
                            try
                            {
                                foreach (PropertyInfo pi in fWindow.GetType().GetProperties())
                                {
                                    if (pi.CanWrite && ("." + pi.Name).ToUpper() == item.Key.ToUpper())
                                    {
                                        pi.SetValue(fWindow, item.Value.ToString(), null);
                                        break;
                                    }
                                }
                            }
                            catch { /* Ignore, this was nothing but a try */ }
                            break;
                    }
                }
            }
        }

        // Execute a supported method on the form
        public void Method(JObject methodObj)
        {
            // Clean up method
            string method = methodObj["Method"].ToString().Substring(0, 1).ToUpper() + methodObj["Method"].ToString().Substring(1).ToLower();

            switch (method)
            {
                case "Show":
                    fWindow.Show();
                    break;

                case "Run":
                    Application.Run(fWindow);
                    break;

                default:
                    MessageBox.Show("Not executing '" + method + "' on " + this.Name); 
                    break;
            }
        }
    }
}
